/* 由 ecpg 处理（回归模式） */
/* 这些包含文件由预处理器添加 */
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* 自动包含部分结束 */
#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))

#line 1 "dt_test2.pgc"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <pgtypes_date.h>
#include <pgtypes_timestamp.h>


#line 1 "regression.h"






#line 8 "dt_test2.pgc"


char *dates[] = { "19990108foobar",
				  "19990108 foobar",
				  "1999-01-08 foobar",
				  "January 8, 1999",
				  "1999-01-08",
				  "1/8/1999",
				  "1/18/1999",
				  "01/02/03",
				  "1999-Jan-08",
				  "Jan-08-1999",
				  "08-Jan-1999",
				  "99-Jan-08",
				  "08-Jan-99",
				  "08-Jan-06",
				  "Jan-08-99",
				  "19990108",
				  "990108",
				  "1999.008",
				  "J2451187",
				  "January 8, 99 BC",
				  /*
				   * 在ParseDateTime()中最大化使用空间，包含25
				   * （MAXDATEFIELDS）个字段和128（MAXDATELEN）总长度。
				   */
				  "........................Xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
				  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
				  /* 26个字段 */
				  ".........................aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
				  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
				  NULL };

/* 不与libc的“times”符号冲突 */
static char *times[] = { "0:04",
				  "1:59 PDT",
				  "13:24:40 -8:00",
				  "13:24:40.495+3",
				  "13:24:40.123456123+3",
				  NULL };

char *intervals[] = { "1 minute",
					  "1 12:59:10",
					  "2 day 12 hour 59 minute 10 second",
					  "1 days 12 hrs 59 mins 10 secs",
					  "1 days 1 hours 1 minutes 1 seconds",
					  "1 year 59 mins",
					  "1 year 59 mins foobar",
					  NULL };

int main(void)
{
	/* 执行 SQL 结束声明部分 */
		 
		  
		 
		 
		 
	
#line 62 "dt_test2.pgc"
 date fc_date1 ;
 
#line 63 "dt_test2.pgc"
 timestamp fc_ts1 , fc_ts2 ;
 
#line 64 "dt_test2.pgc"
 char * fc_text ;
 
#line 65 "dt_test2.pgc"
 interval * fc_i1 ;
 
#line 66 "dt_test2.pgc"
 date * fc_dc ;
/*
 * 默认连接: con2
 * 非默认连接: con1
 *
 */
#line 67 "dt_test2.pgc"


	int fc_i, fc_j;
	char *fc_endptr;

	ECPGdebug(1, stderr);

	fc_ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
	fc_text = PGTYPEStimestamp_to_asc(fc_ts1);

	printf("timestamp: %s\n", fc_text);
	PGTYPESchar_free(fc_text);

	fc_date1 = PGTYPESdate_from_timestamp(fc_ts1);
	fc_dc = PGTYPESdate_new();
	*fc_dc = fc_date1;
	fc_text = PGTYPESdate_to_asc(*fc_dc);
	printf("Date of timestamp: %s\n", fc_text);
	PGTYPESchar_free(fc_text);
	PGTYPESdate_free(fc_dc);

	for (fc_i = 0; dates[fc_i]; fc_i++)
	{
		bool fc_err = false;
		fc_date1 = PGTYPESdate_from_asc(dates[fc_i], &fc_endptr);
		if (fc_date1 == INT_MIN) {
			fc_err = true;
		}
		fc_text = PGTYPESdate_to_asc(fc_date1);
		printf("Date[%d]: %s (%c - %c)\n",
					fc_i, fc_err ? "-" : fc_text,
					fc_endptr ? 'N' : 'Y',
					fc_err ? 'T' : 'F');
		PGTYPESchar_free(fc_text);
		if (!fc_err)
		{
			for (fc_j = 0; times[fc_j]; fc_j++)
			{
				int fc_length = strlen(dates[fc_i])
						+ 1
						+ strlen(times[fc_j])
						+ 1;
				char* fc_t = malloc(fc_length);
				sprintf(fc_t, "%s %s", dates[fc_i], times[fc_j]);
				fc_ts1 = PGTYPEStimestamp_from_asc(fc_t, NULL);
				fc_text = PGTYPEStimestamp_to_asc(fc_ts1);
				printf("TS[%d,%d]: %s\n",
				       fc_i, fc_j, errno ? "-" : fc_text);
				PGTYPESchar_free(fc_text);
				free(fc_t);
			}
		}
	}

	fc_ts1 = PGTYPEStimestamp_from_asc("2004-04-04 23:23:23", NULL);

	for (fc_i = 0; intervals[fc_i]; fc_i++)
	{
		interval *fc_ic;
		fc_i1 = PGTYPESinterval_from_asc(intervals[fc_i], &fc_endptr);
		if (*fc_endptr)
			printf("endptr set to %s\n", fc_endptr);
		if (!fc_i1)
		{
			printf("Error parsing interval %d\n", fc_i);
			continue;
		}
		fc_j = PGTYPEStimestamp_add_interval(&fc_ts1, fc_i1, &fc_ts2);
		if (fc_j < 0)
			continue;
		fc_text = PGTYPESinterval_to_asc(fc_i1);
		printf("interval[%d]: %s\n", fc_i, fc_text ? fc_text : "-");
		PGTYPESchar_free(fc_text);

		fc_ic = PGTYPESinterval_new();
		PGTYPESinterval_copy(fc_i1, fc_ic);
		fc_text = PGTYPESinterval_to_asc(fc_i1);
		printf("interval_copy[%d]: %s\n", fc_i, fc_text ? fc_text : "-");
		PGTYPESchar_free(fc_text);
		PGTYPESinterval_free(fc_ic);
		PGTYPESinterval_free(fc_i1);
	}

	return 0;
}
